package com.fyakm.javaprojectanalysis.common.base;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class F_Package {

    /**
     * 包名
     */
    private String name;

    /**
     * 包名（全路径）
     */
    private String packageName;

    /**
     * 父包名（全路径）
     */
    private F_Package parentPackageName;

    /**
     * 包下的包
     */
    private List<F_Package> packageList;

    /**
     * 包下的类
     */
    private List<F_Class> classList;

    /**
     * 向类列表中添加类列表
     * @param classList
     */
    public void addClassToClassList(List<F_Class> classList){
        if(null != this.classList && this.classList.size()>0){
            this.classList.addAll(classList);
        }
    }

    /**
     * 向类列表中添加类
     * @param clazz
     */
    public void addClassToClassList(F_Class clazz){
        if(null == this.classList){
            this.classList = new ArrayList<>();
        }

        this.classList.add(clazz);

    }

    /**
     * 添加包
     * @param aPackage
     */
    public void addPackageToPackageList(F_Package aPackage) {
        if(null == this.packageList){
            this.packageList = new ArrayList<>();
        }
        this.packageList.add(aPackage);
    }

    @Override
    public String toString() {
        return "F_Package{" +
                "name='" + name + '\'' +
                ", packageName='" + packageName + '\'' +
                ", parentPackageName=" + (parentPackageName!=null?parentPackageName.getName():null) +
                ", packageList=" + packageList +
                ", classList=" + classList +
                '}';
    }

    /**
     * 格式化输出
     * @param offset
     * @return
     */
    public static void formatPrint(int offset,F_Package f_package){

        /**
         * 先输出包名
         */
        for (int i=0;i<offset;i++){
            System.out.print("\t");
        }
        System.out.println(f_package.name);

        /**
         * 递归输出包下的包
         */
        if(null != f_package.packageList && f_package.packageList.size()>0){
            for (F_Package children : f_package.packageList) {
                formatPrint(offset+1,children);
            }
        }

        /**
         * 输出包下的类名
         */
        if(null != f_package.classList && f_package.classList.size()>0){
            for (F_Class f_class : f_package.classList) {
                for (int i=0;i<offset+1;i++){
                    System.out.print("\t");
                }
                System.out.println(f_class.getName());
            }
        }

    }

}
